package com.bytebuddytest.annotation;

import org.junit.Assert;
import org.junit.Test;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2023/10/25 14:12
 */
@SuppressWarnings("unused")
public class SimpleTest {

    @Retention(RetentionPolicy.RUNTIME)
    private @interface SimpleAnnotation {
    }

    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    private @interface InheriableAnnotation {
    }

    @SimpleAnnotation
    @InheriableAnnotation
    private static class Parent {
        @SimpleAnnotation
        @InheriableAnnotation
        public void method() {
            System.out.println("parent");
        }
    }

    private static class Child extends Parent {
        @Override
        public void method() {
            System.out.println("proxy");
        }
    }

    @Test
    public void testAnnotationInheritable() {
        SimpleAnnotation simpleAnnotation = Child.class.getAnnotation(SimpleAnnotation.class);
        Assert.assertNull(simpleAnnotation);
        InheriableAnnotation inheriableAnnotation = Child.class.getAnnotation(InheriableAnnotation.class);
        Assert.assertNotNull(inheriableAnnotation);
    }

    @Test
    public void testOverrideMethod() throws Exception {
        Method method = Child.class.getDeclaredMethod("method");
        SimpleAnnotation simpleAnnotation = method.getAnnotation(SimpleAnnotation.class);
        Assert.assertNull(simpleAnnotation);
        InheriableAnnotation inheriableAnnotation = method.getAnnotation(InheriableAnnotation.class);
        Assert.assertNull(inheriableAnnotation);
    }

}
